//
//  AYMCaseManagementController.m
//  ky
//
//  Created by guo3920591 on 16/5/6.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

#import "AYMCaseManagementController.h"
#import "AYMAddCaseDiseaseController.h"
#import "AYMCaseManger.h"
#import "AYMCaseMangerCell.h"
#import "AYMPersonalHeadView.h"
#define AYMFilePath  [ NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"caseMake.data"]
@interface AYMCaseManagementController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UILabel *diseaseLabel;
@property(nonatomic,strong)UIButton *addBtn;
@property(nonatomic,weak)UITableView *cellMangerView;
@property(nonatomic,assign)NSInteger interCount;
@property(nonatomic,strong)NSMutableArray *caseMangerPlist;
@end
@implementation AYMCaseManagementController

-(NSMutableArray *)caseMangerPlist{
    
    if (_caseMangerPlist == nil) {
        
        _caseMangerPlist = [NSKeyedUnarchiver unarchiveObjectWithFile:AYMFilePath];
        
        if (_caseMangerPlist == nil) {
            
            NSArray *arrY = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"AYMCaseManger.plist" ofType:nil]];
            NSMutableArray *arrM = [NSMutableArray array];
            for (NSDictionary *dict in arrY) {
                
                [arrM addObject:[AYMCaseManger casePlistWithDict:dict]];
            }
            _caseMangerPlist = arrM;
        }
        
    }
    
    
    return _caseMangerPlist;
    
}
#pragma mark - 视图生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"病例管理";
    //布局界面
    [self setupUI];
    
    
}

-(void)setupUI{
    
    [self.cellMangerView reloadData];
    self.view.backgroundColor = [UIColor whiteColor];
    
    AYMPersonalHeadView *headView = [[AYMPersonalHeadView alloc] init];
    
    [self.view addSubview:headView];
    
    
    //病例cellView
    UITableView *caseCellView = [[UITableView alloc] init];
    caseCellView.delegate = self;
    caseCellView.dataSource = self;
    caseCellView.separatorStyle = NO;
    [self.view addSubview:caseCellView];
    caseCellView.bounces = NO;
    caseCellView.backgroundColor = [UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1.0];
    caseCellView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [caseCellView registerClass:[AYMCaseMangerCell class] forCellReuseIdentifier:@"caseMagerCell"];
    self.cellMangerView = caseCellView;
    
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
    addButton.tintColor = [UIColor colorWithRed:95.0/255.0 green:192.0/255.0 blue:191/255.0 alpha:1.0];
    [addButton setTitle:@"添加病例" forState:UIControlStateNormal];
    addButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    //添加按钮的点击事件
    [addButton addTarget:self action:@selector(didClickAddButton:) forControlEvents:UIControlEventTouchUpInside];
    addButton.tag = 100;
    [self.view addSubview:addButton];
    
    [headView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left);
        make.right.equalTo(self.view.right);
        make.top.equalTo(self.view.top);
        make.height.equalTo(125);
        
    }];
    
    [caseCellView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left);
        make.right.equalTo(self.view.right);
        make.bottom.equalTo(addButton.top).offset(-20);
        make.top.equalTo(headView.bottom).offset(10);
        
    }];
    
    
    [addButton makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.bottom).offset(-20);
        make.centerX.equalTo(self.view.centerX);
        make.width.equalTo(100);
    }];
    
    
    
}
#pragma mark - tabview数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.caseMangerPlist.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AYMCaseMangerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"caseMagerCell" forIndexPath:indexPath];
    cell.model = self.caseMangerPlist[indexPath.row];
    
    return cell;
    
}
#pragma mark - tabview代理
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AYMCaseManger *mode1 = self.caseMangerPlist[indexPath.row];
    AYMAddCaseDiseaseController *addVc = [[AYMAddCaseDiseaseController alloc] initWithStyle:UITableViewStyleGrouped];
    addVc.models = mode1;
    addVc.countNum1 = ++self.interCount;
    addVc.caseUpDataModel = ^(){
        
        [self.cellMangerView reloadData];
        [NSKeyedArchiver  archiveRootObject:self.caseMangerPlist toFile:AYMFilePath];
        
    };
    
    [self.navigationController pushViewController:addVc animated:YES];
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        return 120;
    }else{
        return 105;
    }
    
}

#pragma mark - 添加按钮点击
-(void)didClickAddButton:(UIButton *)btn{
    AYMAddCaseDiseaseController *addVc = [[AYMAddCaseDiseaseController alloc] initWithStyle:UITableViewStyleGrouped];
    addVc.btn = btn;
    AYMCaseManger *model = self.caseMangerPlist[0];
    
    if ([model.caseName isEqualToString:@""]) {
        
        addVc.models = model;
    }
    
    addVc.caseNameCatrgory = model.caseName;
    
    addVc.countNum1 = ++self.interCount;
    
    addVc.addCaseDiseaseBlock = ^(AYMCaseManger *model,NSInteger countNum){
        [self.caseMangerPlist addObject:model];
        [self.cellMangerView reloadData];
        [NSKeyedArchiver  archiveRootObject:self.caseMangerPlist toFile:AYMFilePath];
    };
    
    addVc.caseUpDataModel = ^(){
        
        [self.cellMangerView reloadData];
        [NSKeyedArchiver  archiveRootObject:self.caseMangerPlist toFile:AYMFilePath];
        
    };
    
    [self.navigationController pushViewController:addVc animated:YES];
    
}





@end
